package com.chris.homecare
{
	import com.chris.homecare.components.InfoPopupComponent;
	import com.chris.homecare.components.NewsLetterPopup;
	import com.chris.homecare.components.TextInputSkin;
	import com.chris.homecare.data.DataUtil;
	import com.chris.homecare.data.Employee;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.data.Job;
	import com.chris.homecare.data.ReviewData;
	import com.chris.homecare.screens.ApplyJobScreen;
	import com.chris.homecare.screens.BusinessScreen;
	import com.chris.homecare.screens.DetailReviewScreen;
	import com.chris.homecare.screens.EmployeeScreen;
	import com.chris.homecare.screens.EventsScreen;
	import com.chris.homecare.screens.IntroScreen;
	import com.chris.homecare.screens.JobScreen;
	import com.chris.homecare.screens.JobsListScreen;
	import com.chris.homecare.screens.LocationScreen;
	import com.chris.homecare.screens.MenuScreen;
	import com.chris.homecare.screens.PatientListScreen;
	import com.chris.homecare.screens.PerformanceScreen;
	import com.chris.homecare.screens.RequestScreen;
	import com.chris.homecare.screens.ReviewListScreen;
	import com.chris.homecare.screens.ServiceDetailScreen;
	import com.chris.homecare.screens.ServicesScreen;
	import com.chris.homecare.screens.TeamScreen;
	import com.chris.homecare.screens.UpdateScreen;
	import com.chris.homecare.screens.UpdatesListScreen;
	import com.chris.homecare.screens.WriteReviewScreen;
	import com.chris.homecare.utils.CommonUtils;
	import com.chris.homecare.utils.ScreenNames;
	
	import flash.desktop.NativeApplication;
	import flash.events.KeyboardEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.engine.CFFHinting;
	import flash.text.engine.ElementFormat;
	import flash.text.engine.FontDescription;
	import flash.text.engine.FontLookup;
	import flash.text.engine.FontPosture;
	import flash.text.engine.FontWeight;
	import flash.text.engine.RenderingMode;
	import flash.ui.Keyboard;
	
	import feathers.controls.Alert;
	import feathers.controls.Button;
	import feathers.controls.ButtonGroup;
	import feathers.controls.ImageLoader;
	import feathers.controls.Label;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.controls.ScrollText;
	import feathers.controls.TextInput;
	import feathers.controls.text.TextBlockTextRenderer;
	import feathers.core.PopUpManager;
	import feathers.display.Scale9Image;
	import feathers.events.FeathersEventType;
	import feathers.motion.transitions.ScreenSlidingStackTransitionManager;
	import feathers.skins.SmartDisplayObjectStateValueSelector;
	import feathers.system.DeviceCapabilities;
	import feathers.textures.Scale9Textures;
	import feathers.themes.MetalWorksMobileTheme;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;
	
	public class ContentView extends ScreenNavigator
	{
		private var menuBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("menu-icon");
		private var backBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("back-btn");
		private var innerMenuBtn:Texture = TextureAtlasManager.getAtlas().getTexture("bulletLogo");
		
		private var startRatSelBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("star_selected");
		private var startRatDeSelBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("star_deselected");
		private var reviewBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("review_btn");
		private var performanceBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("performance_btn");
		private var hireBtnTexture:Texture = TextureAtlasManager.getAtlas().getTexture("hire_btn");
		
		private var theme:MetalWorksMobileTheme;
		
		private static const INTRO_SCREEN:String = "introScreen";
		private static const MENU_SCREEN:String = "menuScreen";
		private static const TEST_SCREEN:String = "testScreen";
		private static const EVENTS_SCREEN:String = "eventsScreen";
		private static const BUSINESS_SCREEN:String = "businessScreen";
		private static const SERVICES_SCREEN:String = "servicesScreen";
		private static const SERVICE_DETAIL_SCREEN:String = "servicesDetailScreen";
		private static const TEAM_SCREEN:String = "teamScreen";
		private static const UPDATES_LIST_SCREEN:String = "updatesListScreen";
		private static const UPDATES_SCREEN:String = "updatesScreen";
		private static const LOCATION_SCREEN:String = "locationScreen";
		private static const PATIENT_LIST_SCREEN:String = "patientListScreen";
		private static const JOBS_LIST_SCREEN:String = "jobsListScreen";
		private static const REQUEST_SCREEN:String = "requestScreen";
		private static const EMPLOYEE_SCREEN:String = "employeeScreen";
		private static const REVIEW_SCREEN:String = "reviewScreen";
		private static const REVIEW_LIST_SCREEN:String = "reviewlistScreen";
		private static const DETAIL_REVIEW_SCREEN:String = "detailReviewScreen";
		private static const WRITE_REVIEW_SCREEN:String = "writeReviewScreen";
		private static const JOB_SCREEN:String = "jobScreen";
		private static const APPLY_JOB_SCREEN:String = "applyJobScreen";
		
		private var transitionManager:ScreenSlidingStackTransitionManager;
		private var infoPopup:InfoPopupComponent;
		private var newsLetterPopup:NewsLetterPopup;
		
		private var utils:CommonUtils;
		
		public function ContentView()
		{
			super();
			
			this.addEventListener(FeathersEventType.INITIALIZE, initializeHandler);
			
			utils = new CommonUtils();
		}
		
		private function initializeHandler(event:Event):void
		{
			NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
			
			super.initialize();
			theme = new MetalWorksMobileTheme();
			
			this.theme.setInitializerForClass(Button, plainBtnInitializer, "plain-btn");
			this.theme.setInitializerForClass(Button, plainBtnIconInitializer, "plain-icon-btn");
			this.theme.setInitializerForClass(Button, menuBtnInitializer, "menu-btn");
			this.theme.setInitializerForClass(Button, backBtnInitializer, "back-btn");
			this.theme.setInitializerForClass(Button, eventBtnInitializer, "event-btn");
			this.theme.setInitializerForClass(Button, jobsBtnInitializer, "jobs-btn");
			this.theme.setInitializerForClass(Button, locationBtnInitializer, "location-btn");
			this.theme.setInitializerForClass(Button, servicesBtnInitializer, "services-btn");
			this.theme.setInitializerForClass(Button, requestBtnInitializer, "request-btn");
			this.theme.setInitializerForClass(Button, updatesBtnInitializer, "updates-btn");
			this.theme.setInitializerForClass(Button, innerMenuBtnInitializer, "inner-menu-btn");
			this.theme.setInitializerForClass(Button, eventCalenderBtnInitializer, "event-cal-btn");
			this.theme.setInitializerForClass(Button, eventListBtnInitializer, "event-list-btn");
			this.theme.setInitializerForClass(Button, eventCategoryBtnInitializer, "event-cat-btn");
			this.theme.setInitializerForClass(Button, eventCalenderBtnInitializer, "event-cal-btn");
			this.theme.setInitializerForClass(Button, startRatBtnInitializer, "start-ratings-btn");
			this.theme.setInitializerForClass(Button, reviewBtnInitializer, "review-btn");
			this.theme.setInitializerForClass(Button, performanceBtnInitializer, "performance-btn");
			this.theme.setInitializerForClass(Button, hireBtnInitializer, "hire-btn");
			this.theme.setInitializerForClass(TextInput, customTextInputInitializer, "custom-text-input");
			this.theme.setInitializerForClass(ScrollText, customEventPopupScrollTxtInitializer, "custom-evt-scroll-txt");
			this.theme.setInitializerForClass(ScrollText, ourServicesDetailTxtInitializer, "our-ser-scroll-txt");
			this.theme.setInitializerForClass(Alert, customAlertInitializer);
			this.theme.setInitializerForClass(ButtonGroup, customAlertButtonGroupInitializer, "custom-alert-btn");
			
			this.theme.setInitializerForClass(Label, groupListHeadLabel, "grouplist-head-label");
			this.theme.setInitializerForClass(Label, screenHeadingLabel, "screen-heading-label");
			this.theme.setInitializerForClass(Label, employeeNameLabel, "employee-name-label");
			this.theme.setInitializerForClass(Label, employeeTitleLabel, "employee-title-label");
			this.theme.setInitializerForClass(Label, breadcrumbLabel1, "breadcrumb-label1");
			this.theme.setInitializerForClass(Label, breadcrumbLabel2, "breadcrumb-label2");
			this.theme.setInitializerForClass(Label, breadcrumbLabel3, "breadcrumb-label3");
			this.theme.setInitializerForClass(Label, reviewListLabelInitializer, "review-list-lbl");
			this.theme.setInitializerForClass(Label, reviewListTitleInitializer, "review-list-title-lbl");
			this.theme.setInitializerForClass(Label, formHeadingInitializer, "form-heading-lbl");
			this.theme.setInitializerForClass(Label, noMessageLabelInitializer, "no-message-lbl");
			this.theme.setInitializerForClass(Label, updatesHeadingInitializer, "updates-heading-lbl");
			this.theme.setInitializerForClass(Label, eventHeadingInitializer, "event-heading-lbl");
			this.theme.setInitializerForClass(Label, newsLetterPopupLabelInitializer, "news-letter-label");
			this.theme.setInitializerForClass(Label, errorLabelInitializer, "error-label");
			
			this.addScreen(INTRO_SCREEN, new ScreenNavigatorItem(IntroScreen,
				{
					complete:MENU_SCREEN
					
				}));
			this.addScreen(MENU_SCREEN, new ScreenNavigatorItem(MenuScreen, 
				{
					complete:mainMenuHandler
				}));
			this.addScreen(EVENTS_SCREEN, new ScreenNavigatorItem(EventsScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN
				}));
			this.addScreen(BUSINESS_SCREEN, new ScreenNavigatorItem(BusinessScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN
				}));
			this.addScreen(SERVICES_SCREEN, new ScreenNavigatorItem(ServicesScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN,
					triggerServiceDetailScreen:SERVICE_DETAIL_SCREEN
				}));
			this.addScreen(SERVICE_DETAIL_SCREEN, new ScreenNavigatorItem(ServiceDetailScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: SERVICES_SCREEN
				}));
			this.addScreen(UPDATES_LIST_SCREEN, new ScreenNavigatorItem(UpdatesListScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN,
					triggerUpdateScreen: UPDATES_SCREEN
				}));
			this.addScreen(UPDATES_SCREEN, new ScreenNavigatorItem(UpdateScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: UPDATES_LIST_SCREEN
				}));
			this.addScreen(LOCATION_SCREEN, new ScreenNavigatorItem(LocationScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN,
					triggerChangePatient: PATIENT_LIST_SCREEN 
				}));
			this.addScreen(PATIENT_LIST_SCREEN, new ScreenNavigatorItem(PatientListScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: LOCATION_SCREEN,
					change:LOCATION_SCREEN
				}));
			this.addScreen(REQUEST_SCREEN, new ScreenNavigatorItem(RequestScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: MENU_SCREEN
				}));
			this.addScreen(TEAM_SCREEN, new ScreenNavigatorItem(TeamScreen,
				{
					complete:MENU_SCREEN,
					employeeData: navigateEmployeeScreen,
					triggerBackBtn: MENU_SCREEN
				}));
			this.addScreen(EMPLOYEE_SCREEN, new ScreenNavigatorItem(EmployeeScreen,
				{
					complete:MENU_SCREEN,
					triggerPerformance: navigatePerformanceScreen,
					triggerBackBtn: TEAM_SCREEN
				}));
			this.addScreen(REVIEW_SCREEN, new ScreenNavigatorItem(PerformanceScreen,
				{
					complete:MENU_SCREEN,
					triggerManagersReview: navigateManagersReivews,
					triggerCustomerReview: navigateCustomerReivews,
					triggerWriteReview: WRITE_REVIEW_SCREEN,
					triggerBackBtn: EMPLOYEE_SCREEN
				}));
			this.addScreen(REVIEW_LIST_SCREEN, new ScreenNavigatorItem(ReviewListScreen,
				{
					complete:MENU_SCREEN,
					reviewData: reviewDetailScreen,
					triggerBackBtn: REVIEW_SCREEN
				}));
			this.addScreen(DETAIL_REVIEW_SCREEN, new ScreenNavigatorItem(DetailReviewScreen,
				{
					complete:MENU_SCREEN,
					triggerBackBtn: REVIEW_LIST_SCREEN
				}));
			this.addScreen(WRITE_REVIEW_SCREEN, new ScreenNavigatorItem(WriteReviewScreen,
				{
					complete:MENU_SCREEN,
					triggerCustomerReview: navigateCustomerReivews,
					triggerBackBtn: REVIEW_SCREEN
				}));
			this.addScreen(JOBS_LIST_SCREEN, new ScreenNavigatorItem(JobsListScreen,
				{
					complete:MENU_SCREEN,
					triggerJobScreen: navigateJobScreen,
					triggerBackBtn: MENU_SCREEN
				}));
			this.addScreen(JOB_SCREEN, new ScreenNavigatorItem(JobScreen,
				{
					complete:MENU_SCREEN,
					triggerApplyJobScreen: navigateApplyJobScreen,
					triggerBackBtn: JOBS_LIST_SCREEN
				}));
			this.addScreen(APPLY_JOB_SCREEN, new ScreenNavigatorItem(ApplyJobScreen,
				{
					complete:MENU_SCREEN,
					triggerJobListScreen: JOBS_LIST_SCREEN,
					triggerBackBtn: JOB_SCREEN
				}));
			this.transitionManager = new ScreenSlidingStackTransitionManager(this);
			this.transitionManager.duration = 0.4;
			this.showScreen(INTRO_SCREEN);
			
			checkAndShowNewsLetterPopup();
			
			var okBtn:Button = new Button();
			okBtn.label = "OK";
			okBtn.nameList.add("plain-btn");
		}
		
		private function checkAndShowNewsLetterPopup():void
		{
			if(!DataUtil.checkIsUserRegistered())
			{
				newsLetterPopup = new NewsLetterPopup();
				newsLetterPopup.addEventListener(Event.COMPLETE, onNesLetterComplete);
				PopUpManager.forStarling(Starling.current).addPopUp( newsLetterPopup, true, true, function():DisplayObject
				{
					var quad:Quad = new Quad(100, 100, 0x000000);
					quad.alpha = 0.75;
					return quad;
				});
			}
		}
		
		private function groupListHeadLabel(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 42*this.theme.scale, 0xFFFFFF);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function screenHeadingLabel(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*this.theme.scale, 0x5988C0);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function employeeNameLabel(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 42*this.theme.scale, 0x000000);
			
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function newsLetterPopupLabelInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 24*this.theme.scale, 0x000000);
			
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function errorLabelInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 22*this.theme.scale, 0xFF0000);
			
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function employeeTitleLabel(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 24*this.theme.scale, 0x999999);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function breadcrumbLabel1(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*this.theme.scale, 0x000000);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function breadcrumbLabel2(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*this.theme.scale, 0x666666);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function breadcrumbLabel3(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*this.theme.scale, 0x999999);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function reviewListLabelInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*this.theme.scale, 0x111111);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function reviewListTitleInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 22*this.theme.scale, 0xAAAAAA);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function formHeadingInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 28*this.theme.scale, 0x000000);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function noMessageLabelInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 26*this.theme.scale, 0xFF0000);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function updatesHeadingInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 38*this.theme.scale, 0x000000);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function eventHeadingInitializer(label:Label):void
		{
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 28*this.theme.scale, 0x333333);
			label.textRendererProperties.elementFormat = elementFormat;
			label.textRendererProperties.disabledElementFormat = elementFormat;
		}
		
		private function plainBtnInitializer(button:Button):void
		{
			const skinSelector:SmartDisplayObjectStateValueSelector = new SmartDisplayObjectStateValueSelector();
			skinSelector.defaultValue = null;
			skinSelector.defaultSelectedValue = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-downskin"), new Rectangle(10, 10, 30, 30));
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-downskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_DOWN, false);
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-upskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_UP, false);
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-upskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_HOVER, false);
			skinSelector.displayObjectProperties =
				{
					width: 60 * CommonUtils.scaleValue,
						height: 60 * CommonUtils.scaleValue,
						textureScale: CommonUtils.scaleValue
				};
			button.stateToSkinFunction = skinSelector.updateValue;
			
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*CommonUtils.scaleValue, 0x000000);
			button.paddingTop = 5*CommonUtils.scaleValue;
			button.paddingLeft = button.paddingRight = 20*CommonUtils.scaleValue;
			button.defaultLabelProperties.elementFormat = elementFormat;
			button.downLabelProperties.elementFormat = elementFormat;
			button.disabledLabelProperties.elementFormat = elementFormat;
			button.defaultSelectedLabelProperties.elementFormat = elementFormat;
			button.selectedDisabledLabelProperties.elementFormat =elementFormat;
			button.labelFactory = function():TextBlockTextRenderer
			{
				return new TextBlockTextRenderer();
			}
		}
		
		private function plainBtnIconInitializer(button:Button):void
		{
			const skinSelector:SmartDisplayObjectStateValueSelector = new SmartDisplayObjectStateValueSelector();
			skinSelector.defaultValue = null;
			skinSelector.defaultSelectedValue = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-downskin"), new Rectangle(10, 10, 30, 30));
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-downskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_DOWN, false);
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-upskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_UP, false);
			skinSelector.setValueForState(new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("plain-btn-upskin"), new Rectangle(10, 10, 30, 30)), Button.STATE_HOVER, false);
			skinSelector.displayObjectProperties =
				{
					width: 60 * CommonUtils.scaleValue,
						height: 60 * CommonUtils.scaleValue,
						textureScale: CommonUtils.scaleValue
				};
			button.stateToSkinFunction = skinSelector.updateValue;
			
			var icon:ImageLoader = new ImageLoader();
			icon.source = TextureAtlasManager.getAtlas().getTexture("search-icon");
			//the icon will be blurry if it's not on a whole pixel. ImageLoader
			//can snap to pixels to fix that issue.
			icon.snapToPixels = true;
			icon.textureScale = CommonUtils.scaleValue;
			
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.BOLD, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 28*CommonUtils.scaleValue, 0x000000);
			button.paddingTop = 6*CommonUtils.scaleValue;
			button.paddingLeft = 10*CommonUtils.scaleValue;
			button.paddingRight = 20*CommonUtils.scaleValue;
			button.defaultLabelProperties.elementFormat = elementFormat;
			button.downLabelProperties.elementFormat = elementFormat;
			button.disabledLabelProperties.elementFormat = elementFormat;
			button.defaultSelectedLabelProperties.elementFormat = elementFormat;
			button.selectedDisabledLabelProperties.elementFormat =elementFormat;
			button.iconPosition = Button.ICON_POSITION_RIGHT;
			button.iconOffsetX = 10*CommonUtils.scaleValue;
			button.defaultIcon = icon;
			button.labelFactory = function():TextBlockTextRenderer
			{
				return new TextBlockTextRenderer();
			}
		}
		
		private function menuBtnInitializer(button:Button):void
		{
			const menuBtnSkin:Image = new Image(this.menuBtnTexture);
			menuBtnSkin.scaleX = menuBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const menuBtnDownSkin:Image = new Image(this.menuBtnTexture);
			menuBtnDownSkin.alpha = 0.5;
			menuBtnDownSkin.scaleX = menuBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = menuBtnSkin;
			button.downSkin = menuBtnDownSkin;
		}
		
		private function backBtnInitializer(button:Button):void
		{
			const backBtnSkin:Image = new Image(this.backBtnTexture);
			backBtnSkin.scaleX = backBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const backBtnDownSkin:Image = new Image(this.backBtnTexture);
			backBtnDownSkin.alpha = 0.5;
			backBtnDownSkin.scaleX = backBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = backBtnSkin;
			button.downSkin = backBtnDownSkin;
		}
		
		private function eventBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("event-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function jobsBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("jobs-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function locationBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("location-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function servicesBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("ourservice-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function requestBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("request-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function updatesBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("updates-bg"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
		}
		
		private function startRatBtnInitializer(button:Button):void
		{
			const startBtnSkin:Image = new Image(this.startRatDeSelBtnTexture);
			startBtnSkin.scaleX = startBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const startBtnDownSkin:Image = new Image(this.startRatSelBtnTexture);
			startBtnDownSkin.scaleX = startBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = startBtnSkin;
			button.defaultSelectedSkin = startBtnDownSkin;
			button.selectedUpSkin = startBtnDownSkin;
		}
		
		private function reviewBtnInitializer(button:Button):void
		{
			const reviewBtnSkin:Image = new Image(this.reviewBtnTexture);
			const reviewBtnDownSkin:Image = new Image(this.reviewBtnTexture);
			reviewBtnDownSkin.alpha = 0.5;
			reviewBtnSkin.scaleX = reviewBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			reviewBtnDownSkin.scaleX = reviewBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 32*CommonUtils.scaleValue, 0x000000);
			button.defaultLabelProperties.elementFormat = elementFormat;
			button.downLabelProperties.elementFormat = elementFormat;
			button.disabledLabelProperties.elementFormat = elementFormat;
			button.defaultSelectedLabelProperties.elementFormat = elementFormat;
			button.selectedDisabledLabelProperties.elementFormat =elementFormat;
			button.labelFactory = function():TextBlockTextRenderer
			{
				return new TextBlockTextRenderer();
			}
			button.defaultSkin = reviewBtnSkin;
			button.downSkin = reviewBtnDownSkin;
		}
		
		private function performanceBtnInitializer(button:Button):void
		{
			const performanceBtnSkin:Image = new Image(this.performanceBtnTexture);
			const performanceBtnDownSkin:Image = new Image(this.performanceBtnTexture);
			performanceBtnDownSkin.alpha = 0.5;
			performanceBtnSkin.scaleX = performanceBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			performanceBtnDownSkin.scaleX = performanceBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 42*this.theme.scale, 0xFFFFFF);
			button.defaultLabelProperties.elementFormat = elementFormat;
			button.downLabelProperties.elementFormat = elementFormat;
			button.disabledLabelProperties.elementFormat = elementFormat;
			button.defaultSelectedLabelProperties.elementFormat = elementFormat;
			button.selectedDisabledLabelProperties.elementFormat =elementFormat;
			button.labelFactory = function():TextBlockTextRenderer
			{
				return new TextBlockTextRenderer();
			}
			button.defaultSkin = performanceBtnSkin;
			button.downSkin = performanceBtnDownSkin;
		}
		
		private function hireBtnInitializer(button:Button):void
		{
			const hireBtnSkin:Image = new Image(this.hireBtnTexture);
			const hireBtnDownSkin:Image = new Image(this.hireBtnTexture);
			hireBtnDownSkin.alpha = 0.5;
			hireBtnSkin.scaleX = hireBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			hireBtnDownSkin.scaleX = hireBtnDownSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			var fontDes:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			var elementFormat:ElementFormat = new ElementFormat(fontDes, 42*this.theme.scale, 0x000000);
			button.defaultLabelProperties.elementFormat = elementFormat;
			button.downLabelProperties.elementFormat = elementFormat;
			button.disabledLabelProperties.elementFormat = elementFormat;
			button.defaultSelectedLabelProperties.elementFormat = elementFormat;
			button.selectedDisabledLabelProperties.elementFormat =elementFormat;
			button.labelFactory = function():TextBlockTextRenderer
			{
				return new TextBlockTextRenderer();
			}
			button.defaultSkin = hireBtnSkin;
			button.downSkin = hireBtnDownSkin;
		}
		
		private function innerMenuBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(this.innerMenuBtn);
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const eventBtnSelectedSkin:Image = new Image(this.innerMenuBtn);
			eventBtnSelectedSkin.alpha = 0.5
			eventBtnSelectedSkin.scaleX = eventBtnSelectedSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
			button.downSkin = eventBtnSelectedSkin;
		}
		
		private function eventCalenderBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("event-calendar"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const eventBtnSelectedSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("event-calendar"));
			eventBtnSelectedSkin.alpha = 0.5;
			eventBtnSelectedSkin.scaleX = eventBtnSelectedSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
			button.downSkin = eventBtnSelectedSkin;
		}
		
		private function eventListBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("events-list"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const eventBtnSelectedSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("events-list"));
			eventBtnSelectedSkin.alpha = 0.5;
			eventBtnSelectedSkin.scaleX = eventBtnSelectedSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
			button.downSkin = eventBtnSelectedSkin;
		}
		
		private function eventCategoryBtnInitializer(button:Button):void
		{
			const eventBtnSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("events-category"));
			eventBtnSkin.scaleX = eventBtnSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			const eventBtnSelectedSkin:Image = new Image(TextureAtlasManager.getAtlas().getTexture("events-category"));
			eventBtnSelectedSkin.alpha = 0.5;
			eventBtnSelectedSkin.scaleX = eventBtnSelectedSkin.scaleY = DeviceCapabilities.dpi / this.theme.originalDPI;
			
			button.defaultSkin = eventBtnSkin;
			button.downSkin = eventBtnSelectedSkin;
		}
		
		protected function customTextInputInitializer(input:TextInput):void
		{
			var backgroundTexture:Scale9Textures = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("text-box-background"), new Rectangle(8,8,10,10));
			var backgroundSkin:Scale9Image = new Scale9Image(backgroundTexture,1);
			
			backgroundSkin.width = 60 * this.theme.scale;
			backgroundSkin.height = 60 * this.theme.scale;
			input.backgroundSkin = new TextInputSkin();
			
			input.minWidth = input.minHeight = 60 * this.theme.scale;
			input.minTouchWidth = input.minTouchHeight = 88 * this.theme.scale;
			input.gap = 12 * this.theme.scale;
			input.paddingTop = 10 * this.theme.scale;
			input.paddingBottom = 10 * this.theme.scale;
			input.paddingLeft = input.paddingRight = 14 * this.theme.scale;
			input.textEditorProperties.fontFamily = "Helvetica";
			input.textEditorProperties.fontSize = 28 * this.theme.scale;
			input.textEditorProperties.color = 0x000000;
			
			var regularFontDescription:FontDescription = new FontDescription("SourceSansPro", FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF, CFFHinting.NONE);
			input.promptProperties.elementFormat = new ElementFormat(regularFontDescription, 28 * CommonUtils.scaleValue, 0xDDDDDD);
		}
		
		protected function customEventPopupScrollTxtInitializer(scrollText:ScrollText):void
		{
			scrollText.textFormat = new TextFormat("Helvetica", 26*CommonUtils.scaleValue, 0x787878, null, null, null, null, null, TextFormatAlign.JUSTIFY);
			scrollText.isHTML = true;
		}
		
		protected function ourServicesDetailTxtInitializer(scrollText:ScrollText):void
		{
			scrollText.textFormat = new TextFormat("Helvetica", 32*CommonUtils.scaleValue, 0x000000, null, null, null, null, null, TextFormatAlign.JUSTIFY);
			scrollText.isHTML = true;
		}
		
		protected function customAlertInitializer(alert:Alert):void
		{
			alert.paddingTop = 0;
			alert.paddingRight = 24 * CommonUtils.scaleValue;
			alert.paddingBottom = 16 * CommonUtils.scaleValue;
			alert.paddingLeft = 24 * CommonUtils.scaleValue;
			alert.gap = 16 * CommonUtils.scaleValue;
			alert.maxWidth = alert.maxHeight = 560 * CommonUtils.scaleValue;
			
			alert.customHeaderName = "breadcrumb-label1";
			const texture:Scale9Textures = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("popupBackground"), new Rectangle(20,20,60,60));
			alert.backgroundSkin = new Scale9Image(texture);
			
			alert.headerProperties.backgroundSkin = new Scale9Image(texture);
			alert.customButtonGroupName = "custom-alert-btn";
			
			alert.messageProperties
		}
		
		protected function customAlertButtonGroupInitializer(buttonGroup:ButtonGroup):void
		{
			buttonGroup.direction = ButtonGroup.DIRECTION_HORIZONTAL;
			buttonGroup.horizontalAlign = ButtonGroup.HORIZONTAL_ALIGN_CENTER;
			buttonGroup.verticalAlign = ButtonGroup.VERTICAL_ALIGN_JUSTIFY;
			buttonGroup.distributeButtonSizes = false;
			buttonGroup.gap = 12 * CommonUtils.scaleValue;
			buttonGroup.paddingTop = 12 * CommonUtils.scaleValue;
			buttonGroup.paddingRight = 12 * CommonUtils.scaleValue;
			buttonGroup.paddingBottom = 12 * CommonUtils.scaleValue;
			buttonGroup.paddingLeft = 12 * CommonUtils.scaleValue;
			buttonGroup.customButtonName = "plain-btn";
		}
		
		protected function mainMenuHandler(event:Event):void
		{
			CommonUtils.previousScreen = MENU_SCREEN;
			
			if(event.data.page == "Event")
			{
				this.showScreen(EVENTS_SCREEN);
			}
				
			else if(event.data.page == "UpdatesList")
			{
				this.showScreen(UPDATES_LIST_SCREEN);
			}
				
			else if(event.data.page == "Location")
			{
				this.showScreen(LOCATION_SCREEN);
			}
				
			else if(event.data.page == "JobList")
			{
				this.showScreen(JOBS_LIST_SCREEN);
			}
				
			else if(event.data.page == "Request")
			{
				this.showScreen(REQUEST_SCREEN);
			}
				
			else if(event.data.page == "Info")
			{
				if(infoPopup) return;
				infoPopup = new InfoPopupComponent();
				infoPopup.addEventListener(Event.COMPLETE, onInfoButtonClicked);
				infoPopup.width = event.data.width;
				PopUpManager.forStarling(Starling.current).addPopUp( infoPopup, true, true, function():DisplayObject
				{
					var quad:Quad = new Quad(100, 100, 0x000000);
					quad.addEventListener(TouchEvent.TOUCH, removeInfoPopup);
					quad.alpha = 0.75;
					return quad;
				});
			}
		}
		
		private function reviewDetailScreen(event:Event):void
		{
			CommonUtils.previousScreen = ScreenNames.REVIEW_LIST_SCREEN;
			FetchRemoteData.choosenReviewData = ReviewData(event.data);
			this.showScreen(DETAIL_REVIEW_SCREEN);
		}
		
		private function navigateEmployeeScreen(event:Event):void
		{
			CommonUtils.previousScreen = ScreenNames.TEAM_SCREEN;
			FetchRemoteData.choosenEmployeeData = Employee(event.data);
			this.showScreen(EMPLOYEE_SCREEN);
		}
		
		
		private function navigatePerformanceScreen(event:Event):void
		{
			CommonUtils.previousScreen = ScreenNames.EMPLOYEE_SCREEN;
			this.showScreen(REVIEW_SCREEN);
		}
		
		private function navigateManagersReivews(event:Event):void
		{
			CommonUtils.previousScreen = ScreenNames.REVIEW_SCREEN;
			CommonUtils.reviewHeading = "Managers Review";
			FetchRemoteData.currentRatingListCollection = DataUtil.getAllReviewsForEmployee(FetchRemoteData.choosenEmployeeData.id,"1");
			this.showScreen(REVIEW_LIST_SCREEN);
		}
		
		private function navigateCustomerReivews(event:Event):void
		{
			CommonUtils.previousScreen = ScreenNames.REVIEW_SCREEN;
			CommonUtils.reviewHeading = "Customer Review";
			FetchRemoteData.currentRatingListCollection = DataUtil.getAllReviewsForEmployee(FetchRemoteData.choosenEmployeeData.id,"0");
			this.showScreen(REVIEW_LIST_SCREEN);
		}
		
		private function navigateJobScreen(event:Event):void
		{
			FetchRemoteData.choosenJobData = Job(event.data);
			this.showScreen(JOB_SCREEN);
		}
		
		private function navigateApplyJobScreen(event:Event):void
		{
			FetchRemoteData.choosenJobData = Job(event.data);
			this.showScreen(APPLY_JOB_SCREEN);
		}
		
		private function onInfoButtonClicked(event:Event):void
		{
			infoPopup.removeFromParent(true);
			infoPopup = null;
			if(event.data.page == "Business")
			{
				this.showScreen(BUSINESS_SCREEN);
				CommonUtils.previousScreen = ScreenNames.MENU_SCREEN;
			}
				
			else if(event.data.page == "Team")
			{
				this.showScreen(TEAM_SCREEN);
				CommonUtils.previousScreen = ScreenNames.MENU_SCREEN;
			}
				
			else if(event.data.page == "Services")
			{
				this.showScreen(SERVICES_SCREEN);
				CommonUtils.previousScreen = ScreenNames.MENU_SCREEN;
			}
		}
		
		private function removeInfoPopup(event:TouchEvent):void
		{
			var touch:Touch = event.getTouch(event.target as DisplayObject, TouchPhase.BEGAN);
			if (touch)
			{
				event.target.removeEventListener(TouchEvent.TOUCH, removeInfoPopup);
				infoPopup.removeFromParent(true);
				infoPopup = null;
			}
		}
		
		private function onNesLetterComplete():void
		{
			newsLetterPopup.removeFromParent(true);
			newsLetterPopup = null;
		}
		
		private function onKey(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.BACK)
			{
				if(CommonUtils.previousScreen && CommonUtils.previousScreen != "")
					this.showScreen(CommonUtils.previousScreen);
				e.preventDefault();
			}
		}
	}
}